﻿using MySql.Data.Entity;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YTCMS.Data.Configuration;
using YTCMS.Domain.Entities;

namespace YTCMS.Data
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public partial class YTContext : DbContext
    {
        public YTContext(): base("YTCMSContext")
        {
            
        }
        //public YTContext(DbConnection existingConnection, bool contextOwnsConnection)
        //    : base(existingConnection, contextOwnsConnection)
        //{

        //}
        
        public virtual void Commit()
        {
            try
            {
                base.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<OrderItem>().HasKey(o => new {o.OrderId, o.ProductId});
            // modelBuilder.Entity<User>()
            //.HasMany(u => u.Roles)
            //.WithMany(r => r.Users)
            //.Map(m =>
            //{
            //    m.ToTable("RoleMemberships");
            //    m.MapLeftKey("UserName");
            //    m.MapRightKey("RoleName");
            //});
            //modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            //modelBuilder.Entity<users>().ToTable("users").HasKey(s => s.Id).Property(s => s.UserName).IsRequired().HasMaxLength(2);
            //modelBuilder.Configurations.Add(new UserConfiguration());
            base.OnModelCreating(modelBuilder);
        }
    }
}
